package com.smartleanx.module.hrm.service.instation;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageParam;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.module.hrm.context.EmployeeContextHolder;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.esign.EsignPsnInfo;
import com.smartleanx.module.hrm.dal.dataobject.instation.SystemInstationSms;
import com.smartleanx.module.hrm.dal.mysql.esign.ElecCorpMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.EsignPsnInfoMapper;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmETempComponentsMapper;
import com.smartleanx.module.hrm.dal.mysql.instation.SystemInstationSmsMapper;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.intergration.NotifyMessageSendProvider;
import com.smartleanx.module.hrm.service.employee.EmployeeService;
import com.smartleanx.module.hrm.service.esign.ESignUrlConstant;
import com.smartleanx.module.hrm.service.esign.eums.*;
import com.smartleanx.module.hrm.service.esign.service.*;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTaskParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempParam;
import com.smartleanx.module.hrm.service.esign.temp.po.*;
import com.smartleanx.module.hrm.service.esign.temp.result.ComponentBathResult;
import com.smartleanx.module.hrm.service.esign.temp.result.ComponentResult;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmEContractTempResult;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmETaskListResult;
import com.smartleanx.module.hrm.service.esign.temp.vo.FillInLinkVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.FillInViewPDFLinkVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.SignFlowCreateVo;
import com.smartleanx.module.hrm.service.esign.temp.vo.SignFlowParamVo;
import com.smartleanx.module.hrm.util.esign.ESignUtil;
import com.smartleanx.module.hrm.util.esign.EsignHttpResponse;
import com.smartleanx.module.hrm.util.esign.FileUtil;
import com.smartleanx.module.hrm.util.esign.enums.EsignRequestType;
import com.smartleanx.module.hrm.util.esign.enums.FileTypeEnums;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.PostApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostRespDTO;
import com.smartleanx.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
import com.smartleanx.module.system.api.tenant.TenantApi;
import com.smartleanx.module.system.api.tenant.dto.TenantRespDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

@Service
@Slf4j
public class InStationSmsServiceImpl extends ServiceImpl<SystemInstationSmsMapper, SystemInstationSms> implements InStationSmsService {

    @Autowired
    private HrmEContractTaskService hrmEContractTaskService;
    @Autowired
    private HrmEContractTaskActorsService hrmEContractTaskActorsService;
    @Autowired
    private NotifyMessageSendProvider notifyMessageSendProvider;
    @Autowired
    private EmployeeService employeeService;

    @Override
    public PageResult<SystemInstationSms> getInstationPage(PageParam param) {
        IPage<SystemInstationSms> pageParam = new Page<>();
        pageParam.setPages(param.getPageNo());
        pageParam.setSize(param.getPageSize());
        Long employeeId = EmployeeContextHolder.getEmployeeId();
        IPage<SystemInstationSms> page = page(pageParam, new LambdaQueryWrapper<SystemInstationSms>()
                .eq(SystemInstationSms::getEmpId, employeeId)
                .orderByDesc(SystemInstationSms::getCreateTime));
        return new PageResult<>(page.getRecords(),page.getTotal());
    }

    @Override
    public SystemInstationSms getInstationDetail(Long id) {
        return getById(id);
    }

    @Override
    public boolean sendInstationSms(Long taskId, String type) {
        HrmEContractTask hrmEContractTask = hrmEContractTaskService.getById(taskId);
        List<HrmEContractTaskActors> taskActorsList = hrmEContractTaskActorsService.getTaskActorListByTaskId(taskId);
        Map<String, HrmEContractTaskActors> map = taskActorsList.stream().collect(Collectors.toMap(HrmEContractTaskActors::getActorType, Function.identity()));
        HrmEContractTaskActors corpActor = map.get(ActorTypeEnum.CORP.getCode());
        HrmEContractTaskActors personActor = map.get(ActorTypeEnum.PERSON.getCode());


        NotifySendSingleToUserReqDTO notifySendSingleToUserReqDTO = new NotifySendSingleToUserReqDTO();
        EmployeeDO employee = employeeService.getEmployeeById(personActor.getEmpId());
        notifySendSingleToUserReqDTO.setUserId(employee.getUserId());
        notifySendSingleToUserReqDTO.setTemplateCode(type);
        notifySendSingleToUserReqDTO.setTemplateParams(InstationSmsTypeEnum.getMapParam(corpActor.getActorName(),hrmEContractTask.getContractName(),DateUtil.format(hrmEContractTask.getEffectiveDuration(),DatePattern.NORM_DATETIME_FORMAT),type));
        notifyMessageSendProvider.sendSingleNotify(notifySendSingleToUserReqDTO);

        return Boolean.TRUE;
    }
}
